define(function(require, exports, module) {
	"require:nomunge,exports:nomunge,module:nomunge";
	"use strict";
	
	require("./style.css");
	var tp = require("mokit/tp");	
	var htm = require("mokit/ems-text!template.html");
	
	var Class = function(ops){
		this.obj = ops.obj;
		this.onItemClick = ops.onItemClick;
		this.fun = tp.compile(htm);
		var dt = new Date();
		this.thisYear = dt.getFullYear();
		this.thisMonth = dt.getMonth() + 1;
		this.thisDate = dt.getDate();
		this.year = ops.year || this.thisYear;
		this.month = ops.month || this.thisMonth;
		this.date = ops.date || this.thisDate;	
		this.draw();
	};
	
	Class.prototype = {
		setYear: function(e){
			this.year = +e.target.value;
			this.date = 0;
			this.draw();
		},
		setMonth: function(e){
			if(typeof(e)!="object"){
				this.month = +e + this.month;
				if(this.month==0){
					this.month = 12;
					this.year--;
				}else if(this.month==13){
					this.month = 1;
					this.year++;
				}
			}else{
				this.month = +e.target.value;				
			}
			this.date = 0;
			this.draw();
		},
		draw: function(){
			this.day = new Date(this.year, this.month-1, 1).getDay();
			this.days = new Date(this.year,this.month,0).getDate();
			this.obj.innerHTML = this.fun(this);
			this.obj.querySelector(".mlist").addEventListener("change",this.setMonth.bind(this),false);
			this.obj.querySelector(".ylist").addEventListener("change",this.setYear.bind(this),false);
			this.obj.querySelector(".arrow-left").addEventListener("click",this.setMonth.bind(this,-1),false);
			this.obj.querySelector(".arrow-right").addEventListener("click",this.setMonth.bind(this,1),false);
			if(this.onItemClick){
				var me = this;
				[].slice.call(this.obj.querySelectorAll(".day li")).forEach(function(obj){
					var n = +obj.innerHTML;
					n && obj.addEventListener("click",function(){
						me.onItemClick(me.year, me.month, n);
					},false);
				});
			}
		}
	};
	
	module.exports = function(ops){
		return new Class(ops);
	}
	
});